<template>
  <div class="app-container">
    <el-form label-width="120px">
    <template>
  <el-alert
    title="上次更新到："
    type="success">
         第{{this.newchapter.chapterNum}}章  : {{ newchapter.title }} <br>  
  </el-alert>
  <br>
  <br>
</template>

  <el-alert
    title="更新："
    type="success">
         第{{this.newchapter.chapterNum+1}}章：<br>  
  </el-alert>
     
      <br>
         
        
      </el-form-item>

      <el-form-item label="章节标题">
        <el-input v-model="addData.title" placeholder="请输入章节标题"  style="width:200px" />
      </el-form-item>
     
  
      <el-form-item label="类容：">
        <el-input
          type="textarea"
          :rows="40"
          placeholder="请输入内容"
          v-model="addData.content"  style="width:600px;">
        </el-input>
      </el-form-item>

      
      <el-form-item>
        <el-button
          :disabled="saveBtnDisabled"
          type="primary"
          @click="saveOrUpdate()"
          >保存</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import booksShowApi from "@/api/booksShowApi";
import releaseApi from "@/api/releaseApi";
import ImageCropper from "@/components/ImageCropper";
import PanThumb from "@/components/PanThumb";

export default {
  components: { PanThumb, ImageCropper },
  data() {
    return {   
      newchapter:{},
      addData: {
        bookId:'',
        chapterBun:'',
        content:'',
        title:'',
      },
      saveBtnDisabled: false, // 保存按钮是否禁用,
      imagecropperShow: false, //头像上传组件是否显示
      imagecropperKey: 0,
      BASE_API: process.env.BASE_API, //获取dev.env.js里面的地址
    };
  },
  created() {
    //页面渲染之前执行
    this.init();
    this.getnewChapter(this.$route.params.id);
  },
  watch: {
    //监听
    $route(to, from) {
      //路由变化方式，路由发生变化，方法就会执行
      this.init();
    },
  },
  methods: {
     getnewChapter(bookId){
        booksShowApi.getnewChapter(bookId).then((response) => {
          
        console.log(response);
        this.newchapter = response.data;
        console.log(this.newchapter);
        });
     },

    close() {
      //关闭上传头像组件
      this.imagecropperShow = false;
      //上传失败后，重新打开上传组件时初始化组件，否则显示上一次的上传结果
      this.imagecropperKey = this.imagecropperKey + 1;
    },
    cropSuccess(data) {
      //上传成功后返回的图片地址
      console.log(data)
      this.addData.image = data.url;
      console.log(this.addData.image);
      //上传头像成功
      this.imagecropperShow = false;
      //上传成功后，重新打开上传组件时初始化组件，否则显示上一次的上传结果
      this.imagecropperKey = this.imagecropperKey + 1;
    },
    init() {
      //判断路径有id值,做修改
      if (this.$route.params && this.$route.params.id) {
        //从路径获取id值
        const id = this.$route.params.id;
        //调用根据id查询的方法
      } else {
        //路径没有id值，做添加
        //清空表单
        this.slideShow = {};
      }
    },
   
    saveOrUpdate() {
      //判断修改还是添加
      //根据teacher是否有id
      if (!this.addData.id) {
        //添加
        this.addbooks();
      } else {
        //修改
        this.updateActivity();
      }
    },
    //修改轮播图的方法
    updateActivity() {
      activityShow.updateActivityShow(this.addData).then((response) => {
        //提示信息
        this.$message({
          type: "success",
          message: "修改成功!",
        });
        //回到列表页面 路由跳转
        this.$router.push("/");
      });
    },
    //添加轮播图的方法
    addbooks() {
      this.addData.bookId=this.$route.params.id;
      this.addData.chapterNum=this.newchapter.chapterNum+1;
      
      releaseApi.addnewChapter(this.addData).then((response) => {
        //添加成功
        //提示信息
        this.$message({
          type: "success",
          message: "添加成功!",
        });
        //回到列表页面 路由跳转
        this.$router.push( "/" );
      });
    },
  },
};
</script>
